<?php
/*
 * questionSet控制器  
 * Tobey
 * */
require_once "baseController.class.php";
class questionSetController extends baseController{

	/**
     * 所有试卷的展示
     * get   展示模版
     * ajax  获取信息   cla_id 教师   cla_id  学生
     * */
	public function quiz(){
		if(IS_GET) {
			$this->display();
		} else if(IS_AJAX && IS_LOCAL && isset($_POST["cla_id"])){
            session_start();
            $state = $_SESSION["isTeacher"];            //获取身份信息  true表示是教师
            $id = $_SESSION["id"];          //只有学生使用这个id  因此这个id是学生得id
            session_write_close();
            if($state == "true"){       //教师身份
 			    $data = $this->classPower();           //查看权限
                if ($data["status"] == 1) {        
                    if(isset($_POST["np"]) && is_numeric($_POST["np"])){
                        $p = $_POST["np"];
                    } else{
                        $p = 1;
                    }
                    $sql = D();
 				    // 获取分页信息
                    if(IS_PC){
                        $num = 20;
                    } else{
                        $num = 5;
                    }
                    $d["pageInfo"] = $sql->table(array("question_set"=>""))->where(array("class_cla_id"=>$_POST["cla_id"]))->page($p,$num);
                    if($d["pageInfo"]["count"]){
                    //获取试卷
                        $d["quiz"] = $sql->table(array("question_set"=>""))->where(array("class_cla_id"=>$_POST["cla_id"]))->field(array("qus_id","qus_name"," qus_status","qus_remark","qus_create_time"))->order("qus_create_time desc")->limit(($d["pageInfo"]["np"]-1)*$d["pageInfo"]["num"],
                            $d["pageInfo"]["num"])->select();
                        $class = $sql->table(array("class"=>""))->where(array("cla_id"=>$_POST["cla_id"]))->field(array("cla_stu_num"))->find();
                        for ($i = 0; $i < count($d["quiz"]); $i++) {
                            $d["quiz"][$i]["commitNum"] = $sql->table(array("student_result"=>""))->where(array("qus_id"=>$d["quiz"][$i]["qus_id"]))->count();              //获得提交人数
                            $d["quiz"][$i]["lackNum"] = $class["cla_stu_num"] - $d["quiz"][$i]["commitNum"]  ;     //计算缺交人数
                            $total = $sql->table(array("question"=>""))->where(array("qus_id"=>$d["quiz"][$i]["qus_id"]))->field(array("que_score"))->sum();
                            $d["quiz"][$i]["passNum"] = $sql->table(array("student_result"=>""))->where(array("qus_id"=>$d["quiz"][$i]["qus_id"],"stu_score"=>array(">",$total*0.6)))->count();
                        }
                    } else{
                        $d["quiz"] = array();
                    }
                    $data = $this->message(1,"获取试卷信息成功!",$d);
                    unset($d,$class,$sql,$p);
                }
            } else{     //学生身份获取信息
                $data = $this->stuClassPower();
                if($data["status"] == 1){
                    if(isset($_POST["np"]) && is_numeric($_POST["np"])){
                        $p = $_POST["np"];
                    } else{
                        $p = 1;
                    }
                    $sql = D();
                    // 获取分页信息
                    if(IS_PC){
                        $num = 20;
                    } else{
                        $num = 5;
                    }
                    $d["pageInfo"] = $sql->table(array("question_set"=>""))->where(array("class_cla_id"=>$_POST["cla_id"]))->page($p,$num);
                    if($d["pageInfo"]["count"]){
                    //获取试卷
                        $d["quiz"] = $sql->table(array("question_set"=>""))->where(array("class_cla_id"=>$_POST["cla_id"],"qus_status"=>array("!=",0)))->field(array("qus_id","qus_name"," qus_status","qus_remark","qus_create_time"))->order("qus_create_time desc")->limit(($d["pageInfo"]["np"]-1)*$d["pageInfo"]["num"],
                            $d["pageInfo"]["num"])->select();
                        $quiz_num = count($d["quiz"]);
                        for ($i = 0; $i < $quiz_num; $i++) {
                            if($sql->table(array("student_result"=>""))->where(array("qus_id"=>$d["quiz"][$i]["qus_id"],"stu_id"=>$id))->count() > 0){
                                $d["quiz"][$i]["commit"] = "已完成";
                            } else{
                                $d["quiz"][$i]["commit"] = "未交";
                            }
                            $stu_score = $sql->table(array("student_result"=>""))->where(array("qus_id"=>$d["quiz"][$i]["qus_id"],"stu_id"=>$id))->field(array("stu_score"))->find();
                            if($stu_score){
                                $d["quiz"][$i]["stu_score"] = $stu_score["stu_score"];
                            } else{
                                $d["quiz"][$i]["stu_score"] = 0;
                            }
                        }
                    } else{
                        $d["quiz"] = array();
                    }
                    $data = $this->message(1,"获取试卷信息成功!",$d);
                    unset($d,$class,$sql,$p);
                }
            }
            $this->ajaxReturn($data);
            unset($data);
        }
    }

    /**
     * 获取某个试卷的内容
     * ajax   qus_id
     */
    public function getQuizInfo(){
        if(IS_AJAX && IS_LOCAL && isset($_POST["qus_id"])){
            session_start();
            $state = $_SESSION["isTeacher"];
            $id = $_SESSION["id"];
            session_write_close();
            if($state == "true"){
                $data = $this->quizPower();
                if($data["status"] == 1){
                    $data = $this->message(1,"获取试卷信息成功!",D()->table(array("question_set"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->find());
                }
            } else{
                $data = $this->stuQuizPower();
                if($data["status"] == 1){
                    $sql = D();
                    $quiz = $sql->table(array("question_set"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->find();
                    if($quiz){
                        if($sql->table(array("student_result"=>""))->where(array("qus_id"=>$_POST["qus_id"],"stu_id"=>$id))->count()){
                            $quiz["commit"] = "已交";
                        } else{
                            $quiz["commit"] = "未交";
                        }
                    }
                    $data = $this->message(1,"获取试卷信息成功!",$quiz);
                    unset($sql,$quiz);
                }
            }
            $this->ajaxReturn($data);
            unset($data,$state,$id);
        }
    }

    /**
     * 新增试卷
     * ajax
     * */
    public function add(){
    	session_start();
    	if(IS_AJAX && IS_LOCAL && isset($_SESSION["id"])){
    		if(!isset($_POST["qus_remark"]) || trim($_POST["qus_remark"]) == ""){
    			$_POST["qus_remark"] = "如题所述";
    		}
    		$sql = D();
    		if($sql->table(array("questionSet"=>""))->check($_POST,1)){
    			if($sql->table(array("question_set"=>""))->add($_POST)){
    				$data = $this->message(1,"新增试卷成功!","");
    			} else{
    				$data = $this->message(0,"新增试卷失败!","");
    			}
    		} else{
    			$data = $this->message(0,$sql->getCheckError(),"");
    		}
    		unset($sql);
    	} else{
    		$data = $this->message(0,"非法操作!!","");
    	}
    	$this->ajaxReturn($data);
    	unset($data,$qs_sql);
        session_write_close();
    }

    /**
     * 修改试卷
     * ajax  qus_id     qus_name   qus_remark
     * */
    public function update(){
    	if(IS_AJAX && IS_LOCAL && isset($_POST["qus_id"])){
    		$data = $this->quizPower();
    		if($data["status"] == 1) {
    			if(!isset($_POST["qus_remark"]) || trim($_POST["qus_remark"])==""){
    				$_POST["qus_remark"] = "如题所述";
    			}
    			$sql = D();
    			$update = array("qus_name"=>$_POST["qus_name"],"qus_remark"=>$_POST["qus_remark"]);
    			if($sql->table(array("questionSet"=>""))->check($update,2)){
    				if($sql->table(array("question_set"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->update($update)){
    					$data = $this->message(1,"修改试卷信息成功!","");
    				} else{
    					$data = $this->message(0,"试卷信息没有任何变化,无需修改!","");
    				}
    			} else{
    				$data = $this->message(0,$sql->getCheckError(),"");
    			}
    			unset($sql,$update);
    		}
    		$this->ajaxReturn($data);
    		unset($data);
    	}
    }

    /**
     * 检测试卷是否有题目
     * ajax   qus_id
     * */
    public function checkDelete(){
    	if(IS_AJAX && IS_LOCAL && isset($_POST["qus_id"])) {
    		$data = $this->quizPower();
    		if($data["status"] == 1) {
    			if(D()->table(array("question"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->count() > 0){
    				$data = $this->message(1,"该试卷拥有题目,是否同意删除!?","");
    			} else{
    				$data = $this->message(1,"确定删除该试卷的所有信息吗!?","");
    			}
    		}
    	} else{
    		$data = $this->message(0,"非法操作!!","");
    	}
    	$this->ajaxReturn($data);
    	unset($data);
    }

    /**
     * 删除试卷   
     * ajax   qus_id
     * */
    public function delete(){
    	if(IS_AJAX && IS_LOCAL && isset($_POST["qus_id"])) {
    		$data = $this->quizPower();
    		if($data["status"] == 1) {
    			if(D()->table(array("question_set"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->delete()){
    				$data = $this->message(1,"删除试卷成功!","");
    			} else{
    				$data = $this->message(0,"删除试卷失败!","");
    			}
    		}
    	} else{
    		$data = $this->message(0,"非法操作!!","");
    	}
    	$this->ajaxReturn($data);
    	unset($data);
    }

    /**
     * 获得其他班级的试卷基本信息
     * ajax  cla_id
     * */
    public function getOtherClassQuiz(){
    	if(IS_AJAX && IS_LOCAL && isset($_POST["cla_id"])){
    		$data = $this->classPower();
    		if($data["status"] == 1) {
    			$data = $this->message(1,"获取试卷成功!",D()->table(array("question_set"=>""))->where(array("class_cla_id"=>$_POST["cla_id"]))->field(array("qus_id","qus_name","qus_remark"))->select());
    		}
    	} else{
    		$data = $this->message(0,"非法操作!!","");
    	}
    	$this->ajaxReturn($data);
    	unset($data);
    }


    /**
     * 开始或停用试卷
     * ajax
     * */
    public function changeStatus(){
    	if(IS_AJAX && IS_LOCAL && isset($_POST["qus_id"])) {
            $data = $this->quizPower();
            if ($data["status"] == 1) {
                $where["qus_id"] = $_POST["qus_id"];
                $sql = D();
                $result = $sql->table(array("question_set"=>""))->where($where)->field(array("qus_status","class_cla_id","qus_name"))->find();
                if($result) {
                    if ($result["qus_status"] == 0 || $result["qus_status"] == 2) {           //未启用或已结束,要改成启用
                    	$update["qus_status"] = 1;
                    	if ($sql->table(array("question_set"=>""))->where($where)->update(array("qus_status"=>1))) {
                            //启用就要发信息通告
                            $student = $sql->table(array("class_student"=>""))->where(array("class_cla_id"=>$result["class_cla_id"],"is_pass"=>1))->field(array("student_stu_id"))->select();
                            $class = $sql->table(array("class"=>""))->where(array("cla_id"=>$result["class_cla_id"]))->field(array("cla_name"))->find();
                            if($student){
                                $message["m_status"] = 0;       //状态未读
                                $message["m_date"] = date("Y-m-d H:i:s",time());
                                $message["m_content"] = "发布了新测试 ".$result['qus_name'];
                                $message["cla_id"] = $result["class_cla_id"];
                                $table = array("message"=>"");
                                $stu_len = count($student);
                                for($i=0;$i<$stu_len;$i++){
                                    $message["stu_id"] = $student[$i]["student_stu_id"];
                                    $sql->table($table)->add($message);
                                }
                                unset($message,$table,$stu_len);
                            }
                            $data = $this->message(1, "试卷启用生效!", "");
                        } else {
                            $data = $this->message(0, "试卷启用失败!", "");
                        }
                    } else if ($result["qus_status"] == 1) {          //启用,要改成已结束
                        if($sql->table(array("question"=>""))->where(array("qus_id"=>$_POST["qus_id"],"que_type"=>array("in",array(0,1,2,3)),"__string__"=>"(que_answer = '' or que_answer is null)"))->count() == 0){
                            if ($sql->table(array("question_set"=>""))->where($where)->update(array("qus_status"=>2))) {
                                $data = $this->message(1, "试卷停用生效!", "");
                            } else {
                                $data = $this->message(0, "试卷停用失败!", "");
                            }
                        } else{
                            $data = $this->message(0,"试卷中部分题目没有答案,无法结束考试!","");
                        }
                    }
                } else{
                 $data = $this->message(0, "试卷停用失败!", "");
             }
             unset($where,$sql,$result);
         }
         $this->ajaxReturn($data);
         unset($data);
     }
 }


	/**
  	 * 析构函数不用写任何内容也是可以的
  	 */
	public function __destruct(){}
}
